package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RequestMapping("/admin/common")
@Slf4j
@RestController
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;
    @PostMapping("/upload")
    public Result upload(MultipartFile file){
        //获取文件名
        String originalFilename = file.getOriginalFilename();
        log.info("上传的文件名:{}", originalFilename);
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //调用AliOssUtil上传文件
        String url = null;
        try {
            String objectName = UUID.randomUUID().toString()+suffix;
            url = aliOssUtil.upload(file.getBytes(), objectName);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return Result.error("上传文件失败");
        }
        //返回图片路径结果
        return Result.success(url);
    }
}
